//
//  FQDHomeFeedCellItemProvider.m
//  FakeQDaily
//
//  Created by mewxiv on 8/14/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDHomeFeedCellItemProvider.h"
#import "FQDRestModel.h"
#import "QDailyAPI.h"
#import "FQDFeedCache.h"
#import "FQDAppearHistory.h"

// 单元格数据
#import "FQDRegularFeedCellItem.h"
#import "FQDColumnFeedCellItem.h"
#import "FQDRegularADFeedCellItem.h"
#import "FQDHeadlineFeedCellItem.h"


#import "YYModel.h"

static NSString *const kHomeFeedCacheId = @"homefeed";

@implementation FQDHomeFeedCellItemProvider
@dynamic cellItems;

#pragma mark - Override methods
- (void)getCellItems:(NSString *)cursorKey completion:(QDailyAPICompleteBlock)completion
{
    [[QDailyAPI sharedAPI] getHomeFeed:cursorKey completion:completion];
}

- (void)parseResponse:(id)response isLatest:(BOOL)isLatest
{
    if (isLatest) {
        self.banners = [NSArray yy_modelArrayWithClass:[FQDFeed class]
                                                  json:response[@"banners"]];
    }
    
    self.response_feeds = [NSArray yy_modelArrayWithClass:[FQDFeed class]
                                                     json:response[@"feeds"]];
    self.columns = [NSArray yy_modelArrayWithClass:[FQDColumn class]
                                              json:response[@"columns"]];
    
    self.headline = [FQDHeadline yy_modelWithJSON:response[@"headline"]];
    self.banners_ad = [NSArray array];  // 未获取到相关数据，不清楚类型
    self.feeds_ad = [NSArray yy_modelArrayWithClass:[FQDAdFeed class]
                                               json:response[@"feeds_ad"]];
}

- (void)populateNewCellItemsTo:(NSMutableArray *)originalCellItems
{
    NSInteger columnCount = [self.columns count];
    NSInteger feedCount = [self.response_feeds count];
    NSInteger feedIndex = 0;
    NSInteger feedPerGroup = floor((CGFloat) feedCount / columnCount);

    if (self.headline.post) {
        BOOL hasAppeared = [[FQDAppearHistory sharedHistory] qdIDHasAppeared:self.headline.post.ID];
        if (!hasAppeared) {
            FQDHeadlineFeedCellItem *cellItem = [FQDHeadlineFeedCellItem cellItemWithRestModel:self.headline];
            [originalCellItems addObject:cellItem];
        }
    }

    NSInteger adFeedIndex = 0;
    NSInteger adFeedsCount = [self.feeds_ad count];
    for (FQDFeed *feed in self.response_feeds) {

        // 插入广告
        if (adFeedIndex < adFeedsCount) {
            FQDAdFeed *adFeed = self.feeds_ad[adFeedIndex];
            NSInteger adLoc = adFeed.advertisement.ad_location;
            if (feedIndex == adLoc) {
                FQDRegularADFeedCellItem *cellItem = [FQDRegularADFeedCellItem cellItemWithRestModel:adFeed];
                [originalCellItems addObject:cellItem];
                adFeedIndex += 1;
                feedIndex += 1;
            }
        }

        // 添加普通的信息流
        FQDFeedCellItem *cellItem = [FQDRegularFeedCellItem cellItemWithRestModel:feed];
        [originalCellItems addObject:cellItem];
        
        feedIndex += 1;
        
        // 插入栏目内容
        if (feedIndex % feedPerGroup == 0) {
            NSInteger columnIndex = feedIndex / feedPerGroup - 1;
            FQDColumn *column = self.columns[columnIndex];
            FQDColumnFeedCellItem *columnCellItem = [FQDColumnFeedCellItem cellItemWithRestModel:column];
            [originalCellItems addObject:columnCellItem];
        }

    }

    if (feedIndex % feedPerGroup != 0) {
        NSInteger nextColumn = ceil((CGFloat)feedIndex / feedPerGroup) - 1;
        for (NSInteger columnIndex = nextColumn; columnIndex < columnCount; columnIndex++) {
            FQDColumn *column = self.columns[columnIndex];
            FQDColumnFeedCellItem *columnCellItem = [FQDColumnFeedCellItem cellItemWithRestModel:column];
            [originalCellItems addObject:columnCellItem];
        }
    }
//    [originalCellItems addObjectsFromArray:self.columns];
//    [originalCellItems addObjectsFromArray:self.response_feeds];
}

- (NSString *)cacheId
{
    return kHomeFeedCacheId;
}

@end
